package com.lkm.market2_0java.help;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.LongSerializationPolicy;

import java.lang.reflect.Type;

public class GsonHelp {

    public static Gson getGson() {

        return gson;
    }

    public final static Gson gson;

    static {

        GsonBuilder gsonBuilder = new GsonBuilder();


        gsonBuilder.serializeSpecialFloatingPointValues();

//     long到科学计数法的解决方法 。 System.out.println(new Gson().fromJson("{\"username\":\"tomcat\",\"uuid\":123456789012}",Map.class).get("uuid"));
        gsonBuilder.setLongSerializationPolicy(LongSerializationPolicy.STRING);


        gson = gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS").create();
        
    }

    

    public final static JsonArray emptyJsonArray = new JsonArray();
    public final static JsonObject emptyJsonObject = new JsonObject();

    

    public static boolean isArrayJson(String json) {
        if (json == null)
            return false;
        return json.matches("^\\[.*\\]$");
    }

    public static JsonObject toJsonObject(Object object) {
        return toJsonObject(gson, object);
    }




//        TypeAdapter oa = ObjectTypeAdapter.FACTORY.create(gson, TypeToken.get(Object.class));




    

    public static JsonObject toJsonObject(Gson gson, Object object) {
        if (object == null)
            return null;
        try {
            return (JsonObject) gson.toJsonTree(object);
        } catch (Exception e) {
            return null;
        }
    }

    

    public static JsonArray toJsonArray(Object object) {
        return toJsonArray(gson, object);
    }

    




//        TypeAdapter oa = ArrayTypeAdapter.FACTORY.create(gson, TypeToken.get(object.getClass()));



    public static JsonArray toJsonArray(Gson gson, Object object) {
        if (object == null)
            return null;
        try {
            return (JsonArray) gson.toJsonTree(object);
        } catch (Exception e) {
            return null;
        }
    }

    public static JsonArray toJsonArray(Object object, Type type) {
        if (object == null)
            return null;
        try {
            return (JsonArray) gson.toJsonTree(object, type);
        } catch (Exception e) {
            return null;
        }
    }

    public static <T> T fromJson(String json, Type classOfT) {
        return fromJson(gson, json, classOfT);
    }

    public static <T> T fromJson(JsonElement json, Type classOfT) {
        try {
            return gson.fromJson(json, classOfT);
        } catch (Exception e) {
            return null;
        }
    }

    

    public static <T> T fromJson(Gson gson, String json, Type classOfT) {
        try {
            return gson.fromJson(json, classOfT);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String toJson(Object src) {
        try {
            return gson.toJson(src);
        } catch (Exception e) {
            return null;
        }
    }
}